using System;
using Controls;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using Visuals;

namespace UI
{
    public class GameOverUi : MonoBehaviour
    {
        private enum State
        {
            Idle,
            WaitingForContinue,
            WaitingForRestart
        }
        
        [SerializeField] private PlayerController playerController;
        
        [SerializeField] private TextMeshProUGUI pressToContinue;
        [SerializeField] private TextMeshProUGUI pressToRestart;
        [SerializeField] private GameObject cross;
        [SerializeField] private GameObject scoreTitle;
        [SerializeField] private GameObject title;

        [SerializeField] private ScreenShaker screenShaker;
        
        private UiFader _uiFader;

        private State _state = State.Idle;


        private void Awake()
        {
            _uiFader = GetComponent<UiFader>();
            enabled = false;
            
            cross.SetActive(false);
            pressToContinue.gameObject.SetActive(false);
            pressToRestart.gameObject.SetActive(false);
            scoreTitle.gameObject.SetActive(false);
            title.gameObject.SetActive(false);
        }

        private void OnEnable()
        {
            playerController.RestartPerformed += PlayerController_RestartPerformed;
        }

        public void Show()
        {
            _uiFader.Show();
            enabled = true;
            
            scoreTitle.SetActive(true);
            cross.SetActive(true);
            pressToContinue.gameObject.SetActive(true);
            _state = State.WaitingForContinue;
        }
        
        private void PlayerController_RestartPerformed()
        {
            switch (_state)
            {
                case State.Idle:
                    return;
                case State.WaitingForContinue:
                    _state = State.WaitingForRestart;
                    screenShaker.Shake();
                    title.SetActive(true);
                    pressToContinue.gameObject.SetActive(false);
                    pressToRestart.gameObject.SetActive(true);
                    break;
                case State.WaitingForRestart:
                    SceneManager.LoadScene(0);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}
